import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/admin',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    // redirect: '/home',  // 重定向到主页
    children: [
      { path: '403', name: 'Auth', meta: { name: '无权限' }, component: () => import('../views/manager/Auth.vue') },
      { path: '/admin/home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home.vue') },
      { path: '/admin/admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin.vue') },
      { path: '/admin/user', name: 'User', meta: { name: '用户信息' }, component: () => import('../views/manager/User.vue') },
      { path: '/admin/person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/manager/Person.vue') },
      { path: '/admin/password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password.vue') },
      { path: '/admin/notice', name: 'Notice', meta: { name: '公告信息' }, component: () => import('../views/manager/Notice.vue') },
      { path: '/admin/echarts', name: 'Echarts', meta: { name: '信息' }, component: () => import('../views/manager/Echarts.vue') },
      { path: '/admin/type', name: 'Type', meta: { name: '信息' }, component: () => import('../views/manager/Type.vue') },
      { path: '/admin/item', name: 'Item', meta: { name: '信息' }, component: () => import('../views/manager/Item.vue') },
      { path: '/admin/comment', name: 'Comment', meta: { name: '信息' }, component: () => import('../views/manager/Comment.vue') },
      { path: '/admin/poet', name: 'Poet', meta: { name: '信息' }, component: () => import('../views/manager/Poet.vue') },
      { path: '/admin/message', name: 'Message', meta: { name: '信息' }, component: () => import('../views/manager/Message.vue') },
      { path: '/admin/category', name: 'Category', meta: { name: '信息' }, component: () => import('../views/manager/Category.vue') },
      { path: '/admin/locale/info', name: 'LocaleInfo', meta: { name: '地点信息' }, component: () => import('../views/manager/LocaleInfo.vue') },
      { path: '/admin/relation/info', name: 'RelationInfo', meta: { name: '关系信息' }, component: () => import('../views/manager/RelationInfo.vue') },
      { path: '/admin/chart/pyramid', name: 'ChartPyramid', meta: { name: '人口金字塔' }, component: () => import('../views/manager/chart/ChartPyramid.vue') },
      { path: '/admin/chart/number', name: 'ChartNumberYear', meta: { name: '历年人数统计' }, component: () => import('../views/manager/chart/ChartNumberYear.vue') },
      { path: '/admin/relation/poet', name: 'RelationPoet', meta: { name: '人物关系' }, component: () => import('../views/manager/relation/RelationPoet.vue') },
    ]
  },
  {
    path: '/',
    name: 'Front',
    component: () => import('../views/front/Front.vue'),
    redirect: '/front/home',  // 重定向到主页
    children: [
      { path: '/front/front', name: 'Front', meta: { name: '' }, component: () => import('../views/front/Front.vue') },
      { path: '/front/home', name: 'Home', meta: { name: '首页信息' }, component: () => import('../views/front/Home.vue') },
      { path: '/front/person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/front/Person.vue') },
      { path: '/front/password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/front/Password.vue') },
      { path: '/front/type', name: 'Type', meta: { name: '信息' }, component: () => import('../views/front/Type.vue') },
      { path: '/front/items', name: 'Items', meta: { name: '信息' }, component: () => import('../views/front/Items.vue') },
      { path: '/front/itemDetail', name: 'ItemDetail', meta: { name: '信息' }, component: () => import('../views/front/ItemDetail.vue') },
      { path: '/front/poet', name: 'Poet', meta: { name: '信息' }, component: () => import('../views/front/Poet.vue') },
      { path: '/front/poetDetail', name: 'PoetDetail', meta: { name: '信息' }, component: () => import('../views/front/PoetDetail.vue') },
      { path: '/front/message', name: 'Message', meta: { name: '信息' }, component: () => import('../views/front/Message.vue') },
      { path: '/front/collect', name: 'Collect', meta: { name: '信息' }, component: () => import('../views/front/Collect.vue') },
      // { path: '/front/analysis/home', name: 'AnalysisHome', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/AnalysisHome.vue') },
      { path: '/front/analysis/populationStatistics', name: 'PopulationStatistics', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/conponents/PopulationStatistics.vue') },
      { path: '/front/analysis/populationMigration', name: 'PopulationStatistics', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/conponents/PopulationMigration.vue') },
      { path: '/front/analysis/populationDieAddress', name: 'PopulationStatistics', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/conponents/PopulationDieAddress.vue') },
      { path: '/front/analysis/populationSurname', name: 'PopulationDieAddress', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/conponents/PopulationSurname.vue') },
      { path: '/front/analysis/populationRelationship', name: 'PopulationRelationship', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/conponents/PopulationRelationship.vue') },
      { path: '/front/analysis/populationRelationshipNetwork', name: 'PopulationRelationshipNetwork', meta: { name: '历史人物' }, component: () => import('@/views/front/analysis/conponents/NetworkGraph.vue') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/Register.vue') },
  { path: '*', name: '404', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // to 是到达的路由信息
  // from 是开源的路由信息
  // next 是帮助我们跳转路由的函数
  let adminPaths = ['/user']
  let user = JSON.parse(localStorage.getItem('user') || '{}')

  console.log(
      adminPaths
  )
  if (user.role !== '管理员' && adminPaths.includes(to.path)) {
    // 如果当前登录的用户不是管理员，然后当前的到达的路径是管理员才有权限访问的路径，那这个时候我就让用户去到一个没有权限的页面，不让他访问实际的页面
    next('/auth')
  } else {
    next()
  }
})

export default router
